package com.ruoyi.cms.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.cms.domain.CmsSiteBanner;
import com.ruoyi.cms.domain.CmsSiteInfo;
import com.ruoyi.cms.domain.CmsSiteModule;
import com.ruoyi.cms.domain.CmsSiteVisitorMessage;
import com.ruoyi.cms.mapper.CmsSiteBannerMapper;
import com.ruoyi.cms.service.ICmsSiteBannerService;
import com.ruoyi.cms.service.ICmsSiteInfoService;
import com.ruoyi.cms.service.ICmsSiteModuleService;
import com.ruoyi.cms.service.ICmsSiteVisitorMessageService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
public class CmsWebController extends BaseController{
    @Autowired
    private ICmsSiteInfoService cmsSiteInfoService;
    @Autowired
    private ICmsSiteBannerService cmsSiteBannerService;
    @Autowired
    private ICmsSiteModuleService cmsSiteModuleService;
    @Autowired
    private ICmsSiteVisitorMessageService cmsSiteVisitorMessageService;
    @Autowired
    private CmsSiteBannerMapper cmsSiteBannerMapper;


    private CmsSiteInfo handPubData(Model model) {

        CmsSiteInfo info = cmsSiteInfoService.selectUseCmsSiteInfo();
        CmsSiteBanner cmsSiteBanner = new CmsSiteBanner();
        cmsSiteBanner.setModuleId((long)1);
        List<CmsSiteBanner> head = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取头部信息
        cmsSiteBanner.setModuleId((long)2);
        List<CmsSiteBanner> footer = cmsSiteBannerService.selectCmsSiteBannerList(cmsSiteBanner);//获取尾部信息

        model.addAttribute("info", info);
        model.addAttribute("head", head);//进行封装
        System.out.println(JSON.toJSON(head));
        model.addAttribute("footer", footer);
        System.out.println(JSON.toJSON(footer));
        return info;
    }

    @GetMapping("/index")
    public String index(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));

        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "index";
    }


    @GetMapping("/blog")
    public String blog(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "blog";
    }


    @GetMapping("/contact")
    public String contact(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "contact";
    }


    @GetMapping("/aboutus")
    public String aboutus(Model model) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            paramBanner.setModuleId(m.getId());
            paramBanner.setStatus(Constants.SUCCESS);
            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "aboutus";
    }


    @GetMapping("/cms_message")
    @ResponseBody
    public int insertAbout(CmsSiteVisitorMessage cmsSiteVisitorMessage){
        return cmsSiteVisitorMessageService.insertCmsSiteVisitorMessage(cmsSiteVisitorMessage);
    }




    @GetMapping("/pub_head")
    public String pubHead(Model model,@RequestParam(value = "searchKey",required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
//        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
//        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索

        List<CmsSiteBanner>  result=cmsSiteBannerMapper.selectCmsSiteBannerBySearch("%"+searchKey+"%");
//        List<Map<String, Object>> moduleInfos = new ArrayList<>();
//        for (CmsSiteModule m : modules) {
//            CmsSiteBanner paramBanner = new CmsSiteBanner();
//            if(searchKey!=null){
//                paramBanner.setTitle(searchKey);
//            }
//
//            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
//            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
//            Map<String, Object> r = new HashMap<>(3);
//            r.put("type", m.getValue());
//            r.put("module", m);
//            r.put("banners", banners);
//            moduleInfos.add(r);
//        }
//        model.addAttribute("modules", moduleInfos);
        model.addAttribute("result", result);
//        System.out.println(JSON.toJSON(moduleInfos));
        return "sous";
    }


    @GetMapping("/pub_footer")
    public String pubFooter(Model model,@RequestParam(value = "searchKey",required = false) String searchKey) {
        CmsSiteInfo info = handPubData(model);
        String moduleType = info.getModuleType();
        CmsSiteModule paramsModule = new CmsSiteModule();
        paramsModule.setType(moduleType);
        paramsModule.setStatus(Constants.SUCCESS);
        List<CmsSiteModule> modules = cmsSiteModuleService.selectCmsSiteModuleList(paramsModule);
        modules.sort(Comparator.comparing(CmsSiteModule::getSort));
        //搜索
        List<Map<String, Object>> moduleInfos = new ArrayList<>();
        for (CmsSiteModule m : modules) {
            CmsSiteBanner paramBanner = new CmsSiteBanner();
            if(searchKey!=null){
                paramBanner.setTitle(searchKey);
            }

            List<CmsSiteBanner> banners = cmsSiteBannerService.selectCmsSiteBannerList(paramBanner);
            banners.sort(Comparator.comparing(CmsSiteBanner::getSort));
            Map<String, Object> r = new HashMap<>(3);
            r.put("type", m.getValue());
            r.put("module", m);
            r.put("banners", banners);
            moduleInfos.add(r);
        }
        model.addAttribute("modules", moduleInfos);
        System.out.println(JSON.toJSON(moduleInfos));
        return "sous";
    }





    /**
     * 获取文章的分页信息
     *
     * @return
     */
    @GetMapping("/cms_articles")
    @ResponseBody
    public TableDataInfo articlePage(@RequestParam(value = "pageSize",defaultValue = "7")Integer pageSize,
                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){

        PageUtils.startPage(pageNum,pageSize);
        Long itemId = Convert.toLong(ServletUtils.getParameter("itemId"));
        CmsSiteBanner param = new CmsSiteBanner();
        if (itemId != null && itemId != 0) {
            param.setItemId(itemId);

        }
        param.setModuleId((long)9);//查指定模块的数据
        PageUtils.orderBy("sort desc,id desc");
        List<CmsSiteBanner> data = cmsSiteBannerService.selectCmsSiteBannerList(param);
        TableDataInfo tableDataInfo = getDataTable(data);
        System.out.println(JSON.toJSON(tableDataInfo));
        return tableDataInfo;
    }


    @GetMapping("/page")
    public String page(Model model,@RequestParam("id")long id) {
        handPubData(model);
        CmsSiteBanner cmsSiteBanner = cmsSiteBannerService.selectCmsSiteBannerById(id);
        model.addAttribute("data",cmsSiteBanner);
        System.out.println(JSON.toJSON(cmsSiteBanner));
        return "page";
    }
}
